package com.example.smartstart.controller;


import com.alibaba.excel.EasyExcel;
import com.example.smartstart.entity.dto.StudentInfoDTO;
import com.example.smartstart.model.Result;
import com.example.smartstart.entity.StudentInfo;
import com.example.smartstart.entity.StudentInfoReadListener;
import com.example.smartstart.model.MyResponseData;
import com.example.smartstart.service.IStudentInfoService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("studentinfo/")
public class StudentInfoController {
    @Autowired
    private IStudentInfoService iStudentInfoService;

    // 查询所有学生信息
    @GetMapping("list")
    public List<StudentInfo> listAllStudentInfo() {
        return iStudentInfoService.listAllStudentInfo();
    }

    // 根据学号查询学生信息
    @GetMapping("getStudentInfo")
    public Result<StudentInfo> getStudentInfoById(/*@RequestHeader(name = "Authorization") String token*/String studentId) {
//        System.out.println("------执行请求-----");
//        Map<String, Object> map = JwtUtil.parseToken(token);
//        String studentId = (String) map.get("studentId");
        StudentInfo studentInfo = iStudentInfoService.getStudentInfoById(studentId);
//        System.out.println("=============返回的值========");
        System.out.println(studentInfo);
        return Result.success(studentInfo);
    }

    // 导出Excel
    @GetMapping("export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("studentinfo", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), StudentInfo.class).sheet("学生信息").doWrite(iStudentInfoService.listAllStudentInfo());
    }

    // 导入Excel
    @PostMapping("import")
    public void importExcel(MultipartFile file) throws IOException {

        // 读取文件的流
        InputStream is = file.getInputStream();

        // 创建一个监听器
        StudentInfoReadListener listener = new StudentInfoReadListener(iStudentInfoService);

        EasyExcel.read(is,
                        StudentInfo.class,
                        listener)
                .sheet(0)// 读第几个工作表，0开始
                .headRowNumber(1)// 列头从第几行开始
                .doRead();

    }

    // 学生报到
    @PostMapping("report")
    public Result updateInfo(@RequestBody StudentInfo studentInfo){
        String phone = studentInfo.getPhone();
        String qqNumber = studentInfo.getQqNumber();
        String email = studentInfo.getEmail();
        String grateSchool = studentInfo.getGraduateSchool();
        String origin = studentInfo.getOrigin();
        String address = studentInfo.getAddress();
        String photo = studentInfo.getPhoto();
        String studentId = studentInfo.getStudentId();
        iStudentInfoService.updateInfo(phone,qqNumber,email,grateSchool,origin,address,photo,studentId);
        return Result.success();
    }

    // 分页查询学生信息
    @GetMapping("listByPage")
    public MyResponseData listByPage(StudentInfo studentInfo){
        MyResponseData data = new MyResponseData();
        data.setErrorCode(0);
        data.setMessage("success");
        int row = iStudentInfoService.queryTotal(studentInfo);
        System.out.println(row);
        data.setArg1(iStudentInfoService.queryTotal(studentInfo));
        data.setList(iStudentInfoService.listByPage(studentInfo));
        System.out.println(data.getArg1());
        return data;
    }

    // 添加和修改学生信息
    @PostMapping("addoredit")
    public Result addOrEditInfo(StudentInfo studentInfo){
        studentInfo.setOffset(null);
        studentInfo.setPageSize(null);
        boolean res = iStudentInfoService.saveOrUpdate(studentInfo);
        if (res) {
            return Result.success();
        }else {
            return Result.error("操作失败");
        }
    }

    // 删除学生信息
    @DeleteMapping("delete")
    public Result deleteInfo(String studentId){
        int row = iStudentInfoService.deleteInfo(studentId);
        if (row>=1){
            return Result.success();
        }else {
            return Result.error("操作失败");
        }
    }

    // 查询男生女生人数、报到人数
    @GetMapping("getStudentInfoCount")
    public Result getStudentInfoCount(){
        return Result.success(iStudentInfoService.getStudentInfoCount());
    }

    // 查询各班级报到人数
    @GetMapping("getClassReportCount")
    public Result<List<StudentInfoDTO>> getClassReportCount(){
        return Result.success(iStudentInfoService.getClassReportCount());
    }

    // 查询各学院人数
    @GetMapping("getCollegeStudentSCount")
    public Result<List<StudentInfoDTO>> getCollegeStudentSCount(){
        return Result.success(iStudentInfoService.getCollegeStudentSCount());
    }

    // 查询各系别人数
    @GetMapping("getDepartmentStudentSCount")
    public Result<List<StudentInfoDTO>> getDepartmentStudentSCount(){
        return Result.success(iStudentInfoService.getDepartmentStudentSCount());
    }
}




